package com.bookstore.framework.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Swagger重定向配置
 * 将旧的Swagger UI路径重定向到新的路径
 */
@Configuration
public class SwaggerRedirectConfig implements WebMvcConfigurer {

    @Value("${server.servlet.context-path:/}")
    private String contextPath;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 将旧的swagger-ui.html路径重定向到新的swagger-ui/index.html
        registry.addRedirectViewController("/swagger-ui.html", "/swagger-ui/index.html");
        // 将根路径重定向到Knife4j文档页面
        registry.addRedirectViewController("/", "/doc.html");
        
        // 考虑context-path的情况
        String normalizedContextPath = contextPath.endsWith("/") ? contextPath : contextPath + "/";
        if (!"/".equals(normalizedContextPath)) {
            registry.addRedirectViewController(normalizedContextPath + "swagger-ui.html", 
                                              normalizedContextPath + "swagger-ui/index.html");
            registry.addRedirectViewController(normalizedContextPath, 
                                              normalizedContextPath + "doc.html");
        }
    }
}